package mall.dao;

import mall.model.bo.MallAddOrderBO;
import mall.model.bo.MallSendContentBO;
import mall.model.bo.MallSettleAccountsBO;
import mall.model.tempob.CommentInfoFromOrder;
import mall.model.tempob.OrderInfoFromSpec;
import mall.model.tempob.OrderInfoFromUser;
import mall.model.vo.MallGetOrderByStateVO;
import mall.model.vo.MallOrderGoodsInfoVO;
import mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-06-01 21:09
 *
 * */
public class MallOrderDaoImpl implements MallOrderDao {

    @Override
    public OrderInfoFromUser getOrderInfoFromUser(String token) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        OrderInfoFromUser query = null;
        try {
            query = queryRunner.query("select id as userId, recipient as name, address, phone from user where nickname = ?",
                    new BeanHandler<OrderInfoFromUser>(OrderInfoFromUser.class), token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public OrderInfoFromSpec getOrderInfoFromSpec(Integer goodsDetailId) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        OrderInfoFromSpec query = null;
        try {
            query = queryRunner.query("select goodsId, specName as spec from spec where id = ?",
                    new BeanHandler<OrderInfoFromSpec>(OrderInfoFromSpec.class), goodsDetailId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public String getOrderInfoFromGoods(Integer goodsId) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Object query = null;
        try {
            query = queryRunner.query("select name from goods where id = ?",
                    new ScalarHandler<>(), goodsId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query.toString();
    }

    @Override
    public void addOrder(MallAddOrderBO addOrderBO, OrderInfoFromUser user, OrderInfoFromSpec spec, String goods) {
        //获取当前时间
        java.util.Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curDate = dateFormat.format(date);
        QueryRunner queryRunner = new QueryRunner();
        try {
            queryRunner.update(DruidUtils.getConnection(true),"insert into orders values (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    user.getUserId(), addOrderBO.getToken(), user.getName(), user.getAddress(),
                    user.getPhone(), goods, spec.getGoodsId(), spec.getSpec(), addOrderBO.getGoodsDetailId(),
                    addOrderBO.getNum(), addOrderBO.getAmount(), addOrderBO.getState(),
                    false, curDate, curDate);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<MallGetOrderByStateVO> getOrderByStateFromOrders(String state, String token) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<MallGetOrderByStateVO> query = null;
        try {
            query = queryRunner.query("select id, stateId as state, num as goodsNum, amount, goodsDetailId, createtime, hasComment from orders where stateId = ? and nickname = ?",
                    new BeanListHandler<MallGetOrderByStateVO>(MallGetOrderByStateVO.class),
                    state, token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public OrderInfoFromSpec getSpecInfo(Integer goodsDetailId) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        OrderInfoFromSpec query = null;
        try {
            query = queryRunner.query("select goodsId, specName as spec from spec where id = ?",
                    new BeanHandler<OrderInfoFromSpec>(OrderInfoFromSpec.class), goodsDetailId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public MallOrderGoodsInfoVO getOderByStateFromGoods(Integer goodsId) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        MallOrderGoodsInfoVO query = null;
        try {
            query = queryRunner.query("select id, img, name, price as unitPrice from goods where id = ?",
                    new BeanHandler<MallOrderGoodsInfoVO>(MallOrderGoodsInfoVO.class), goodsId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public void deleteOrder(String id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("delete from orders where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void settleAccounts(List<MallSettleAccountsBO> settleAccountsBOS) {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curDate = dateFormat.format(date);
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        for (MallSettleAccountsBO accountsBO : settleAccountsBOS) {
            try {
                queryRunner.update("update orders set num = ?, amount = ?, stateId = 1, updatetime = ? where id = ?",
                        accountsBO.getGoodsNum(), accountsBO.getAmount(),curDate, accountsBO.getId());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void pay(String id) {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curDate = dateFormat.format(date);
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update orders set stateId = 1, updatetime = ? where id = ?",curDate, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void confirmReceive(String id) {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curDate = dateFormat.format(date);
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update orders set stateId = 3, updatetime = ? where id = ?",curDate, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public CommentInfoFromOrder updateOrderOfHasComment(Integer orderId) {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curDate = dateFormat.format(date);
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update orders set hasComment = true, updatetime = ? where id = ?",curDate, orderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        CommentInfoFromOrder query = null;
        try {
            query = queryRunner.query("select spec as specName, userId from orders where id = ?",
                    new BeanHandler<CommentInfoFromOrder>(CommentInfoFromOrder.class), orderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public void sendComment(MallSendContentBO sendContentBO, CommentInfoFromOrder commentInfo) {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curDate = dateFormat.format(date);
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("insert into comments values (null, ?, ?, ?, ?, ?, ?, ?)",
                    sendContentBO.getGoodsId(), sendContentBO.getContent(), sendContentBO.getToken(),
                    sendContentBO.getScore(), commentInfo.getSpecName(), curDate, commentInfo.getUserId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public String getStockNum(Integer goodsDetailId) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Object query = null;
        try {
            query = queryRunner.query("select stockNum from spec where id = ?",
                    new ScalarHandler<>(), goodsDetailId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query.toString();
    }

    @Override
    public void reduceSpec(Integer goodsDetailId, int i) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update spec set stockNum = ? where id = ?", i, goodsDetailId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
